package com.nondo.rdp.finance.endpoint.api.admin.setting.home;

import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.setting.home.SettingModuleSlide;
import com.nondo.rdp.finance.logic.SettingModule.SettingModuleLogic;
import com.nondo.rdp.finance.model.setting.home.SettingModuleSlideBean;
import com.nondo.rdp.finance.service.setting.home.SettingModuleService;
import com.nondo.rdp.finance.service.setting.home.SettingModuleSlideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController("apiAdminSettingHomeModuleSlide")
@RequestMapping("/api/admin/setting/home/module/slide")
public class ModuleSlideEndpoint {
    @Autowired
    SettingModuleService settingModuleService;
    @Autowired
    SettingModuleSlideService settingModuleSlideService;
    @Autowired
    SettingModuleLogic settingModuleLogic;

    @RequestMapping("/list")
    public View list() {
        View view = new View(20000, "获取列表成功");

        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleSlide> list = this.settingModuleSlideService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleSlide item : list) {
            SettingModuleSlideBean settingModuleSlideBean = new SettingModuleSlideBean();
            BeanUtils.copy(item, settingModuleSlideBean);

            //处理附加数据
            settingModuleSlideBean.setExtra(settingModuleLogic.getAdminTypeContent(item.getType(), item.getValue()));

            //处理图片
            settingModuleSlideBean.setImageFull(CommUtil.getFileUrl(settingModuleSlideBean.getImage()));
            settingModuleSlideBean.setImage(item.getImage());

            newList.add(settingModuleSlideBean);
        }

        view.setData(newList);

        return view;
    }

    @RequestMapping("/add")
    public View add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SettingModuleSlide settingModuleSlide, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(settingModuleSlide.getType(), settingModuleSlide.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }
            this.settingModuleSlideService.save(settingModuleSlide);
        }

        return view;
    }

    @RequestMapping("/update")
    public View update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SettingModuleSlideBean settingModuleSlideBean, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(settingModuleSlideBean.getType(), settingModuleSlideBean.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }

            //数据拷贝
            SettingModuleSlide settingModuleSlide = new SettingModuleSlide();
            BeanUtils.copy(settingModuleSlideBean, settingModuleSlide);

            this.settingModuleSlideService.update(settingModuleSlide);
        }

        return view;
    }

    @RequestMapping("/delete/{id}")
    public View delete(@PathVariable("id") Long id) {
        this.settingModuleSlideService.delete(id);

        return View.ofOk("删除成功");
    }
}